@extends('admin.layout.layout', ['row' => @$page])

@section('contain')


    <div class="portlet">
        <div class="portlet-title">
            <div class="caption">
                <x-portlet-breadcrumb :page="$page"></x-portlet-breadcrumb>
            </div>
            <div class="actions"></div>
        </div>

        <form action="/{{ request()->path() }}/../" method="post" onsubmit="return checkVal(this)" class="distributor-form">
            @csrf
            @method('PUT')
            <h3 class="section_sub_title">{{ ts('baseinformation') }}</h3>
            <div class="form-item form-s1">
                <div class="form-label" required="">{{ ts('SDZH') }}</div>
                <div class="form-input">
                    <div class="input-group custom-control-inline">
                        <input type="text" name="unique_id" value="{{ $data->unique_id }}" class="form-control" readonly>
                    </div>
                </div>
                <div class="text-secondary ml-3 mt-1">{{ ts('DCL1') }}</div>
            </div>
            {{--
            <div class="form-item form-s1">
                <div class="form-label" required="">{{ ts('password') }}</div>
                <div class="form-input">
                    <div class="input-group custom-control-inline">
                        <input type="text" name="password" value="{{ $data->password ?: old('password') }}" class="form-control">
                    </div>
                </div>
            </div>--}}
            <div class="form-item form-s1">
                <div class="form-label" required="">{{ ts('sdqy') }}</div>
                <div class="form-input">
                    <input type="text" name="abbr" value="{{ $data->abbr ?: old('abbr') }}" class="form-control" >
                </div>
            </div>
            <div class="form-item form-s1">
                <div class="form-label" required="">{{ ts('companyname') }}</div>
                <div class="form-input">
                    <input type="text" name="company_name" value="{{ $data->company_name ?: old('company_name') }}" class="form-control" >
                </div>
            </div>


            <div class="form-item form-s1">
                <div class="form-label" required="">{{ ts('SDXZZB') }}</div>
                <div class="form-input">
                    <select name="warranty_create" class="form-control">
                        <option value="0">{{ ts('no') }}</option>
                        <option value="1" {{ (@$data->warranty_create == 1 || old('warranty_create')==1) ? 'selected' : '' }}>{{ ts('yes') }}</option>
                    </select>
                </div>
            </div>

            <div class="form-item form-s1">
                <div class="form-label" required="">{{ ts('PHONENUMBER') }}</div>
                <div class="form-input">
                    <div class=" input-group custom-control-inline">
                        <div class="input-group-prepend">
                            <select name="extension" class="form-control">
                                @foreach(\App\Models\PhoneExtension::get(['id', 'phone_ext']) as $v)
                                    <option value="{{ $v->phone_ext }}" {{ ($data->extension == $v->phone_ext || old('extension')==$v->phone_ext) ? 'selected' : '' }}>{{ $v->phone_ext }}</option>
                                @endforeach
                            </select>
                        </div>
                        <input type="number" name="phone_number" value="{{ (int)$data->phone_number ?: old('phone_number') }}" class="form-control" >
                    </div>
                </div>
            </div>
            <div class="form-item form-s1">
                <div class="form-label">{{ ts('EMAIL') }}</div>
                <div class="form-input">
                    <input type="email" name="email_address" value="{{ $data->email_address ?: old('email_address') }}" class="form-control">
                </div>
            </div>
            <div class="form-item form-s1">
                <div class="form-label" required="">{{ ts('status') }}</div>
                <div class="form-input">
                    <select name="status" class="form-control">
                        <option value="1" {{ ($data->status == 1 || old('status')==1) ? 'selected' : '' }}>{{ ts('active') }}</option>
                        <option value="0" {{ ($data->status != 1 && old('status')!=1) ? 'selected' : '' }}>{{ ts('deactive') }}</option>
                    </select>
                </div>
            </div>
            <div class="form-item form-s1">
                <div class="form-label">{{ ts('ROLES') }}</div>
                <div class="form-input">
                    <select name="role_id" class="form-control">
                        <option value="0">{{ ts('SELECTROLES') }}</option>
                        @if (!empty($roles))
                            @foreach($roles as $role)
                                <option value="{{ $role->id }}" {{ ($data->role_id == $role->id || old('role_id')==$role->id) ? 'selected' : '' }}>{{ $role->simple_chinese_name }}</option>
                            @endforeach
                        @endif
                    </select>
                </div>
            </div>
            <div class="form-item form-s1">
                <div class="form-label" required="">{{ ts('region') }}</div>
                <div class="form-input">
                    <select name="region_id" id="" class="form-control region_json" data-value="{{ $data->region_id }}" >
                        <option value="">{{ ts('select') }}</option>
                        <option value="{{ $data->region_id ?: old('region_id') }}" selected>{{ ts('region_master.'.$data->region_id) }}</option>
                    </select>
                </div>
            </div>
            <div class="form-item form-s1">
                <div class="form-label" required="">{{ ts('province') }}</div>
                <div class="form-input">
                    <select name="province_id" id="" class="form-control province_json province_data" data-value="{{ $data->province_id }}" >
                        <option value="">{{ ts('select') }}</option>
                        <option value="{{ $data->province_id ?: old('province_id') }}" selected>{{ ts('province_master.'.$data->province_id) }}</option>
                    </select>
                </div>
            </div>
            <div class="form-item form-s1">
                <div class="form-label" required="">{{ ts('city') }}</div>
                <div class="form-input">
                    <select name="city" id="" class="form-control city_json" data-value="{{ $data->city }}" >
                        <option value="">{{ ts('select') }}</option>
                        <option value="{{ $data->city ?: old('city') }}" selected>{{ ts('cities.'.$data->city) }}</option>
                    </select>
                </div>
            </div>
            <div class="form-item form-s1">
                <div class="form-label" required="">{{ ts('addresss') }}</div>
                <div class="form-input">
                    <input type="text" name="address" value="{{ $data->address?: old('address') }}" class="form-control">
                </div>
            </div>
            <div class="form-item form-s1">
                <div class="form-label">漆面膜任务值</div>
                <div class="form-input">
                    <input type="text" name="task_value" value="{{ !empty($user) && !empty($user->task_value) ? $user->task_value : old('task_value') }}" class="form-control">
                </div>
            </div>
            <div class="form-item form-s1">
                <div class="form-label">窗面膜任务值</div>
                <div class="form-input">
                    <input type="text" name="win_task_value" value="{{ !empty($user) && !empty($user->win_task_value) ? $user->win_task_value : old('win_task_value') }}" class="form-control">
                </div>
            </div>
            <h3 class="section_sub_title">{{ ts('contactInformation') }}</h3>
            <div class="form-item form-s1">
                <div class="form-label" required="">{{ ts('contactperson') }}</div>
                <div class="form-input">
                    <input type="text" name="first_name" value="{{ $data->first_name ?: old('first_name') }}" class="form-control" >
                </div>
            </div>
            <div class="form-item form-s1">
                <div class="form-label">{{ ts('sjh') }}</div>
                <div class="form-input">
                    <input type="number" name="cell_phone" value="{{ (int)$data->cell_phone ?: old('cell_phone') }}" class="form-control" >
                </div>
            </div>
            <div class="form-item form-s1">
                <div class="form-label">{{ ts('WECHATID') }}</div>
                <div class="form-input">
                    <input type="text" name="wechat_id" value="{{ $data->wechat_id ?: old('wechat_id') }}" class="form-control" >
                </div>
            </div>

            <h3 class="section_sub_title">订货系统信息</h3>
            <div class="form-item form-s1">
                <div class="form-label" required="">店铺编号</div>
                <div class="form-input">
                    <input id="new_store_code" type="text" name="new_store_code" readonly="true" value="{{ $new_store_code ?? old('new_store_code') }}" class="form-control" >
                </div>
            </div>
            <div class="form-item form-s1">
                <div class="form-label" required="">订货联系人</div>
                <div class="form-input">
                    <input type="text" name="order_name" value="{{ $store_info->order_name ?? old('order_name') }}" class="form-control" >
                </div>
            </div>
            <div class="form-item form-s1">
                <div class="form-label" required="">订货手机号</div>
                <div class="form-input">
                    <input type="text" name="order_mobile" value="{{ $store_info->order_mobile ?? old('order_mobile') }}" class="form-control" >
                </div>
            </div>
            <div class="form-item form-s1">
                <div class="form-label" required="">{{ ts('city') }}</div>
                <div class="form-input">
                    <select name="store_city" id="" class="form-control city_data_json" data-value="{{ $store_info->city_id ?? old('city') }}" >
                        <option value="">{{ ts('select') }}</option>
                        @if (!empty($store_info->city_id))
                            <option value="{{ $store_info->city_id ?? old('city') }}" selected>{{ ts('cities.'.$store_info->city_id) }}</option>
                        @endif
                    </select>
                </div>
            </div>
            <div class="form-item form-s1">
                <div class="form-label" required="">收货地址</div>
                <div class="form-input">
                    <input type="text" name="order_address" value="{{ $store_info->order_address ?? old('order_address') }}" class="form-control" >
                </div>
            </div>

            <h3 class="section_sub_title">{{ ts('explain') }}</h3>
            <div class="mt-3" style="width: 650px;max-width: 100%;padding-left: 2rem;">
                <textarea name="note" id="" class="form-control">{{ @$data->note ?: old('note') }}</textarea>
            </div>
            <div class="mt-4">
                <button class="btn btn-primary">{{ ts('submit') }}</button>
            </div>
        </form>

    </div>


@endsection


@section('script')
    <script>
        /*$('body').on('change', 'input[type="email"]', function () {
            var val = $(this).val();
            var email = /^\w{1,}@\w{1,}\.\w{1,}$/;
            if (val) {
                if (!email.test(val)) {
                    alert('邮箱格式不符合');
                    // $(this).focus();
                }
                console.log('email test', email.test(val));
            }
            console.log('email', val);
        })*/
        $("body").on("change", ".province_data", function () {
            console.log($(this).val());
            $('.city_data_json').empty().append(makeSelectOptions(getCityByProvinceId($(this).val())))
            $.ajax({
                url: '/admin/dealer/store_code?province_id=' + $(this).val(),
                data: {},
                dataType: 'json',
                type: 'get',
                success: function (data) {
                    console.log(data)
                    if (data.code != 0) {
                        toastr.error(data.msg)
                        return false;
                    } else {
                        $("#new_store_code").val(data.new_store_code)
                    }

                }
            });
        })
        $(function () {
            let province_id = $('.province_json').val()
            let city_value = $('.city_data_json').data('value');
            if (province_id) {
                $('.city_data_json').empty().append(makeSelectOptions(getCityByProvinceId(province_id), city_value))
            }
        })
        $('form.distributor-form').on('submit', function () {
            var form = formObjV2('form.distributor-form');
            var msg = '';
            if ($(this).find('input[name="unique_id"]').attr('readonly')) {

            } else {
                if (!myTestV1('unique_id', form.unique_id)) {
                    msg = '账号不符合规则！账号规则为CN+（4-7）位数字组成'
                    $(this).find('input[name="unique_id"]').focus();
                }
            }
            if (form.email_address && !myTestV1('email_address', form.email_address)) {
                if (!myTestV1('email', email_address_val)) {
                    msg = '邮箱 格式不符合规范！';
                    $(this).find('input[name="email_address"]').focus();
                }
            }

            if (!form.company_name && !msg) {
                msg = '公司名称 不能为空！';
            }
            if (!form.phone_number && !msg) {
                msg = '联系电话 不能为空！';
            }
            if (!form.region_id && !msg) {
                msg = '区域 不能为空！';
            }
            if (!form.province_id && !msg) {
                msg = '省 不能为空！';
            }
            if (!form.city && !msg) {
                msg = '城市 不能为空！';
            }
            if (!form.address && !msg) {
                msg = '地址 不能为空！';
            }
            if (!form.first_name && !msg) {
                msg = '联系人 不能为空！';
            }

            if (msg) {
                alert(msg);
                return false;
            }  else {
                return true;
            }
        })
    </script>
@endsection
